package com.jilian.powerstation.modul.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jilian.powerstation.R;
import com.jilian.powerstation.listener.CustomItemClickListener;
import com.jilian.powerstation.manege.Weather.ForecastInfo;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class TemDateListAdapter extends RecyclerView.Adapter<TemDateListAdapter.ViewHolder> {

    private List<ForecastInfo.ListBean> mDatas;
    private Context mContext;
    private CustomItemClickListener listener;

    public TemDateListAdapter(List<ForecastInfo.ListBean> mDatas, Context context, CustomItemClickListener listener) {
        this.mDatas = mDatas;
        this.mContext = context;
        this.listener = listener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(mContext).inflate(R.layout.item_tem_date, parent, false);
        ViewHolder viewHolder = new ViewHolder(view, listener);
        return viewHolder;


    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        ForecastInfo.ListBean dto = mDatas.get(position);
        holder.tvTitle.setText(dateToWeek(dto.getDt()) + dateToHour(dto.getDt()));
        holder.tvTitle1.setText(dto.getDt_txt());
        int maxTem = (int) dto.getMain().getTemp_max();
        int minTem = (int) dto.getMain().getTemp_min();
        holder.tvWeaterHeight1.setText(minTem + "°C");
        holder.tvWeaterLow1.setText(maxTem + "°C");
        if (dto.getWeather() != null && dto.getWeather().size() > 0) {
            ForecastInfo.ListBean.WeatherBean weatherBean = dto.getWeather().get(0);
            Glide.with(mContext).
                    load(String.format("https://openweathermap.org/img/w/%s.png", weatherBean.getIcon()))
                    .into(holder.imgWeater1);//
        }
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }


    class ViewHolder extends RecyclerView.ViewHolder {
        private RelativeLayout rlWeater1;
        private TextView tvTitle;
        private TextView tvTitle1;
        private ImageView imgWeater1;
        private TextView tvWeaterHeight1;
        private TextView tvWeaterLow1;


        public ViewHolder(@NonNull View itemView, final CustomItemClickListener listener) {
            super(itemView);
            rlWeater1 = (RelativeLayout) itemView.findViewById(R.id.rl_weater1);
            tvTitle = (TextView) itemView.findViewById(R.id.tv_title);
            tvTitle1 = (TextView) itemView.findViewById(R.id.tv_title1);
            imgWeater1 = (ImageView) itemView.findViewById(R.id.img_weater1);
            tvWeaterHeight1 = (TextView) itemView.findViewById(R.id.tv_weater_height1);
            tvWeaterLow1 = (TextView) itemView.findViewById(R.id.tv_weater_low1);
        }
    }

    public static String dateToWeek(int datetime) {
        Long time = getTime(datetime);
        Calendar cd = Calendar.getInstance();
        cd.setTime(new Date(time));
        int year = cd.get(Calendar.YEAR); //获取年份
        int month = cd.get(Calendar.MONTH); //获取月份
        int day = cd.get(Calendar.DAY_OF_MONTH); //获取日期
        int week = cd.get(Calendar.DAY_OF_WEEK); //获取星期

        String weekString;
        switch (week) {
            case Calendar.SUNDAY:
                weekString = "Sunday";
                break;
            case Calendar.MONDAY:
                weekString = "Monday";
                break;
            case Calendar.TUESDAY:
                weekString = "Tuesday";
                break;
            case Calendar.WEDNESDAY:
                weekString = "Wednesday";
                break;
            case Calendar.THURSDAY:
                weekString = "Thursday";
                break;
            case Calendar.FRIDAY:
                weekString = "Friday";
                break;
            default:
                weekString = "Saturday";
                break;

        }

        return weekString;
    }

    public static String dateToHour(int datetime) {
        Long time = getTime(datetime);
        SimpleDateFormat f = new SimpleDateFormat("HH");
        Date date = new Date(time);
        return f.format(date);
    }

    public static long getTime(String datetime) {
        SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Long time = null;
        try {
            time = f.parse(datetime).getTime();
        } catch (ParseException e) {
            time = 0L;
        }

        return time;
    }
    public static long getTime(int datetime) {
        return datetime*1000L;
    }
}
